/**
 * Created by wangzhenxin on 2017/11/9.
 */

import {Component, Input, Output, OnChanges, OnInit, EventEmitter} from '@angular/core';

@Component({
  selector: 'app-cust-select',
  template: `
    
    <select *ngIf="viewType != 'readonly'" [(ngModel)]="value" class="form-control" (change)="onChange()">
      <option *ngFor="let obj of dataList" value="{{obj.value}}">{{obj.text}}</option>
    </select>
    
    <div *ngIf="viewType == 'readonly'">{{curData?.text}}</div>
    
    `
})

export class CustSelectComponent implements OnInit {

  @Input() value: string;
  @Output() valueChange = new EventEmitter<string>();

  @Input() viewType: string;
  @Input() dataType: string;

  dataList: any[];
  curData: any;

  onChange() {

    this.valueChange.emit(this.value);

  }

  ngOnInit() {

    switch (this.dataType) { // 准备数据

      case 'pathStatus':
        this.dataList = [
          {value: '0', text: '有效'},
          {value: '1', text: '失效'}
        ];
        if (this.viewType === 'select_all') {
          this.dataList.unshift({value: '', text: '全部'});
        }

        break;

      case 'actionType':
        this.dataList = [
          {value: '0', text: '文字'},
          {value: '1', text: '文字加图片'},
          {value: '2', text: '视频'}
        ];
        if (this.viewType === 'select_all') {
          this.dataList.unshift({value: '', text: '全部'});
        }

        break;

      case 'accountStatus':
        this.dataList = [
          {value: '0', text: '可登陆'},
          {value: '1', text: '不可登录'}
        ];
        if (this.viewType === 'select_all') {
          this.dataList.unshift({value: '', text: '全部'});
        }

        break;

      case 'type':
        this.dataList = [
          {value: '0', text: 'android'},
          {value: '1', text: 'ios'}
        ];
        if (this.viewType === 'select_all') {
          this.dataList.unshift({value: '', text: '全部'});
        }

        break;

      case 'isCompelUp':
        this.dataList = [
          {value: '0', text: '可选择升级'},
          {value: '1', text: '强制升级'},
          {value: '2', text: '版本一致'}
        ];
        if (this.viewType === 'select_all') {
          this.dataList.unshift({value: '', text: '全部'});
        }

        break;
    }

    if (this.viewType === 'readonly') {

      this.curData = this.dataList.find(data => data.value == this.value);

    }
  }


}
